# /bin/bash

LOGGING_PATH='/app/logs'
CONFIG_PATH='/app/config'
SPRING_CONF_FILE="${CONFIG_PATH}/application.yaml"

# 加载spring配置文件
SPRING_CONF=''
if [ -f "$SPRING_CONF_FILE" ]; then
    SPRING_CONF="--spring.config.location=${CONFIG_PATH}/application.yaml"
fi

echo "SPRING_CONF: ${SPRING_CONF}"

# 加载JVM参数
JVM_OPS=''
JVM_OPS_FILE="${CONFIG_PATH}/jvm_options.conf"
if [ -f "$JVM_OPS_FILE" ]; then
  JVM_OPS=`cat $JVM_OPS_FILE`
fi
echo "JVM_OPS: ${JVM_OPS}"

# Logback配置文件
LOGGING_CONFIG_FILE="${CONFIG_PATH}/logback-spring.xml"
LOGGING_CONFIG=''
if [ -f "$LOGGING_CONFIG_FILE" ]; then
  LOGGING_CONFIG="--logging.config=${LOGGING_CONFIG_FILE}"
fi
echo "LOGGING_CONFIG: ${LOGGING_CONFIG}"

if [ ! -d "${LOGGING_PATH}" ]; then
  mkdir -p ${LOGGING_PATH}
fi
if [ ! -d "${CONFIG_PATH}" ]; then
  mkdir -p ${CONFIG_PATH}
fi

echo 'Starting application...'

java \
  -Dfile.encoding=UTF8 \
  ${JVM_OPS} \
  -Dnetworkaddress.cache.ttl=60 \
  -Dsun.net.inetaddr.ttl=60 \
  -XX:+HeapDumpOnOutOfMemoryError \
  -XX:+HeapDumpBeforeFullGC \
  -XX:+HeapDumpAfterFullGC \
  -XX:HeapDumpPath=${LOGGING_PATH}/heapdump.bin \
  -jar /app/lib/app.jar \
  -ENV=${ENV} \
  --spring.profiles.active=prod \
  --logging.path=${LOGGING_PATH} \
  ${LOGGING_CONFIG} \
  ${SPRING_CONF}
